﻿namespace TestTool.GUI
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Controls;
    using TestTool.GUI.Data;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Views;
    using TestTool.HttpTransport.Interfaces;

    internal class MainForm : Form, IMainView, IView
    {
        private readonly MainController _controller;
        private ToolStripMenuItem aboutToolStripMenuItem;
        private IContainer components;
        private DevicePage devicePage;
        private DiscoveryPage discoveryPage;
        private const string HELP_FILE_NAME = "ONVIF Device Test Tool Help.chm";
        private ToolStripMenuItem helpToolStripMenuItem;
        private ToolStripMenuItem howDoIToolStripMenuItem;
        private MenuStrip mainMenuStrip;
        private ManagementPage managementPage;
        private ConformanceTestPage setupPage;
        private SplashImage splashImage;
        private TabControl tcPages;
        private TestPage testPage;
        private TabPage tpConformance;
        private TabPage tpDevice;
        private TabPage tpDiscovery;
        private TabPage tpManagement;
        private TabPage tpTest;

        public MainForm()
        {
            this.InitializeComponent();
            this._controller = new MainController(this);
            this._controller.SetChildControllers(this.setupPage.Controller, this.discoveryPage.Controller, this.managementPage.Controller, this.testPage.Controller, this.devicePage.Controller);
            this._controller.ActivateController(this.discoveryPage.Controller);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutForm().ShowDialog();
        }

        public void ActivateManagementPage()
        {
            this.tcPages.SelectedTab = this.tpManagement;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public IController GetController()
        {
            return this._controller;
        }

        private void HideSplashScreen()
        {
            this.splashImage.Visible = false;
            base.MainMenuStrip.Visible = true;
            this.Cursor = Cursors.Default;
            this.tcPages.BringToFront();
        }

        private void howDoIToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo("ONVIF Device Test Tool Help.chm")
                {
                    CreateNoWindow = false,
                    UseShellExecute = true
                };
                Process.Start(startInfo);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(MainForm));
            this.tcPages = new TabControl();
            this.tpDiscovery = new TabPage();
            this.discoveryPage = new DiscoveryPage();
            this.tpManagement = new TabPage();
            this.managementPage = new ManagementPage();
            this.tpConformance = new TabPage();
            this.setupPage = new ConformanceTestPage();
            this.tpTest = new TabPage();
            this.testPage = new TestPage();
            this.tpDevice = new TabPage();
            this.devicePage = new DevicePage();
            this.mainMenuStrip = new MenuStrip();
            this.helpToolStripMenuItem = new ToolStripMenuItem();
            this.howDoIToolStripMenuItem = new ToolStripMenuItem();
            this.aboutToolStripMenuItem = new ToolStripMenuItem();
            this.splashImage = new SplashImage();
            this.tcPages.SuspendLayout();
            this.tpDiscovery.SuspendLayout();
            this.tpManagement.SuspendLayout();
            this.tpConformance.SuspendLayout();
            this.tpTest.SuspendLayout();
            this.tpDevice.SuspendLayout();
            this.mainMenuStrip.SuspendLayout();
            base.SuspendLayout();
            this.tcPages.Controls.Add(this.tpDiscovery);
            this.tcPages.Controls.Add(this.tpManagement);
            this.tcPages.Controls.Add(this.tpConformance);
            this.tcPages.Controls.Add(this.tpTest);
            this.tcPages.Controls.Add(this.tpDevice);
            this.tcPages.Dock = DockStyle.Fill;
            this.tcPages.Location = new Point(0, 0x18);
            this.tcPages.Name = "tcPages";
            this.tcPages.SelectedIndex = 0;
            this.tcPages.Size = new Size(900, 0x268);
            this.tcPages.TabIndex = 0;
            this.tcPages.SelectedIndexChanged += new EventHandler(this.tcPages_SelectedIndexChanged);
            this.tpDiscovery.Controls.Add(this.discoveryPage);
            this.tpDiscovery.Location = new Point(4, 0x16);
            this.tpDiscovery.Name = "tpDiscovery";
            this.tpDiscovery.Padding = new Padding(3);
            this.tpDiscovery.Size = new Size(0x37c, 590);
            this.tpDiscovery.TabIndex = 1;
            this.tpDiscovery.Text = "Discovery";
            this.tpDiscovery.UseVisualStyleBackColor = true;
            this.discoveryPage.DeviceAddress = null;
            this.discoveryPage.Dock = DockStyle.Fill;
            this.discoveryPage.Location = new Point(3, 3);
            this.discoveryPage.Name = "discoveryPage";
            this.discoveryPage.Password = "";
            this.discoveryPage.ServiceAddress = "";
            this.discoveryPage.Size = new Size(0x376, 0x248);
            this.discoveryPage.TabIndex = 0;
            this.discoveryPage.UserName = "";
            this.tpManagement.Controls.Add(this.managementPage);
            this.tpManagement.Location = new Point(4, 0x16);
            this.tpManagement.Name = "tpManagement";
            this.tpManagement.Padding = new Padding(3);
            this.tpManagement.Size = new Size(0x329, 0x1f7);
            this.tpManagement.TabIndex = 2;
            this.tpManagement.Text = "Management";
            this.tpManagement.UseVisualStyleBackColor = true;
            this.managementPage.Dock = DockStyle.Fill;
            this.managementPage.Location = new Point(3, 3);
            this.managementPage.Name = "managementPage";
            this.managementPage.Size = new Size(0x323, 0x1f1);
            this.managementPage.TabIndex = 0;
            this.tpConformance.Controls.Add(this.setupPage);
            this.tpConformance.Location = new Point(4, 0x16);
            this.tpConformance.Name = "tpConformance";
            this.tpConformance.Padding = new Padding(3);
            this.tpConformance.Size = new Size(0x37c, 590);
            this.tpConformance.TabIndex = 0;
            this.tpConformance.Text = "Conformance Test";
            this.tpConformance.UseVisualStyleBackColor = true;
            this.setupPage.Brand = "";
            this.setupPage.Dock = DockStyle.Fill;
            this.setupPage.Location = new Point(3, 3);
            this.setupPage.Model = "";
            this.setupPage.Name = "setupPage";
            this.setupPage.OperatorName = "";
            this.setupPage.OrganizationAddress = "";
            this.setupPage.OrganizationName = "";
            this.setupPage.OtherInformation = "";
            this.setupPage.Size = new Size(0x376, 0x248);
            this.setupPage.TabIndex = 0;
            this.tpTest.Controls.Add(this.testPage);
            this.tpTest.Location = new Point(4, 0x16);
            this.tpTest.Name = "tpTest";
            this.tpTest.Padding = new Padding(3);
            this.tpTest.Size = new Size(0x37c, 590);
            this.tpTest.TabIndex = 3;
            this.tpTest.Text = "Diagnostic";
            this.tpTest.UseVisualStyleBackColor = true;
            this.testPage.Dock = DockStyle.Fill;
            this.testPage.Location = new Point(3, 3);
            this.testPage.Name = "testPage";
            this.testPage.Size = new Size(0x376, 0x248);
            this.testPage.TabIndex = 0;
            this.tpDevice.Controls.Add(this.devicePage);
            this.tpDevice.Location = new Point(4, 0x16);
            this.tpDevice.Name = "tpDevice";
            this.tpDevice.Padding = new Padding(3);
            this.tpDevice.Size = new Size(0x329, 0x1f7);
            this.tpDevice.TabIndex = 5;
            this.tpDevice.Text = "Debug";
            this.tpDevice.UseVisualStyleBackColor = true;
            this.devicePage.CapabilitiesExchange = CapabilitiesExchangeStyle.GetServices;
            this.devicePage.Dock = DockStyle.Fill;
            this.devicePage.Location = new Point(3, 3);
            this.devicePage.Name = "devicePage";
            this.devicePage.Security = Security.Digest;
            this.devicePage.Size = new Size(0x323, 0x1f1);
            this.devicePage.TabIndex = 0;
            this.mainMenuStrip.Items.AddRange(new ToolStripItem[] { this.helpToolStripMenuItem });
            this.mainMenuStrip.Location = new Point(0, 0);
            this.mainMenuStrip.Name = "mainMenuStrip";
            this.mainMenuStrip.RenderMode = ToolStripRenderMode.System;
            this.mainMenuStrip.Size = new Size(900, 0x18);
            this.mainMenuStrip.TabIndex = 1;
            this.mainMenuStrip.Text = "mainMenuStrip";
            this.helpToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { this.howDoIToolStripMenuItem, this.aboutToolStripMenuItem });
            this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
            this.helpToolStripMenuItem.Size = new Size(40, 20);
            this.helpToolStripMenuItem.Text = "Help";
            this.howDoIToolStripMenuItem.Name = "howDoIToolStripMenuItem";
            this.howDoIToolStripMenuItem.Size = new Size(0x8d, 0x16);
            this.howDoIToolStripMenuItem.Text = "How Do I...";
            this.howDoIToolStripMenuItem.Click += new EventHandler(this.howDoIToolStripMenuItem_Click);
            this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
            this.aboutToolStripMenuItem.Size = new Size(0x8d, 0x16);
            this.aboutToolStripMenuItem.Text = "About...";
            this.aboutToolStripMenuItem.Click += new EventHandler(this.aboutToolStripMenuItem_Click);
            this.splashImage.BackColor = Color.Black;
            this.splashImage.Dock = DockStyle.Fill;
            this.splashImage.Location = new Point(0, 0x18);
            this.splashImage.Name = "splashImage";
            this.splashImage.Size = new Size(900, 0x268);
            this.splashImage.TabIndex = 2;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(900, 0x272);
            base.Controls.Add(this.splashImage);
            base.Controls.Add(this.tcPages);
            base.Controls.Add(this.mainMenuStrip);
            this.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0xcc);
            base.Icon = (Icon)manager.GetObject("$this.Icon");
            base.MainMenuStrip = this.mainMenuStrip;
            this.MinimumSize = new Size(900, 0x272);
            base.Name = "MainForm";
            this.Text = "ONVIF Device Test Tool";
            base.Shown += new EventHandler(this.MainForm_Shown);
            base.FormClosing += new FormClosingEventHandler(this.MainForm_FormClosing);
            this.tcPages.ResumeLayout(false);
            this.tpDiscovery.ResumeLayout(false);
            this.tpManagement.ResumeLayout(false);
            this.tpConformance.ResumeLayout(false);
            this.tpTest.ResumeLayout(false);
            this.tpDevice.ResumeLayout(false);
            this.mainMenuStrip.ResumeLayout(false);
            this.mainMenuStrip.PerformLayout();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void InitializeControllers()
        {
            this._controller.Initialize();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this._controller.TestIsRunning())
            {
                if (MessageBox.Show("Test is running. Stop the test and exit?", "Warning!", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    e.Cancel = true;
                }
                else
                {
                    this._controller.StopTest();
                }
            }
            if (this._controller.RequestInProgress())
            {
                this._controller.StopRequest();
            }
            try
            {
                this._controller.SaveContextData();
            }
            catch (Exception)
            {
            }
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            this.ShowSplashScreen();
            this.Cursor = Cursors.AppStarting;
            IAsyncResult result = new Action(this.InitializeControllers).BeginInvoke(null, null);
            new Action<IAsyncResult>(this.WaitMethod).BeginInvoke(result, null, null);
        }

        private void OnPostLoad()
        {
            this._controller.LoadContext();
        }

        public void ShowError(Exception e)
        {
        }

        public void ShowError(string message)
        {
        }

        private void ShowSplashScreen()
        {
            this.mainMenuStrip.Visible = false;
            this.splashImage.Location = new Point(0, 0);
            this.splashImage.Size = new Size(base.Width, base.Height);
            this.splashImage.Update();
        }

        public void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            base.BeginInvoke((Action)delegate
            {
                if ((state == TestTool.GUI.Enums.ApplicationState.Idle) || (state == TestTool.GUI.Enums.ApplicationState.TestPaused))
                {
                    this.Cursor = Cursors.Arrow;
                }
                else
                {
                    this.Cursor = Cursors.AppStarting;
                }
            });
        }

        private void tcPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._controller.ActivateController((this.tcPages.SelectedTab.Controls[0] as IView).GetController());
        }

        private void WaitMethod(IAsyncResult mainAction)
        {
            Thread.Sleep(0xbb8);
            WaitHandle.WaitAll(new WaitHandle[] { mainAction.AsyncWaitHandle });
            base.Invoke(new Action(() => this.OnPostLoad()));
            base.BeginInvoke(new Action(() => this.HideSplashScreen()));
        }
    }
}

